package com.nanyang.tms.service.bill.impl.flow;

import com.nanyang.common.core.flow.context.TransHandlerContext;
import com.nanyang.common.core.flow.convert.TransConvert;
import com.nanyang.common.core.flow.factory.AbstranctTransPipelineFactory;
import com.nanyang.common.core.flow.invoker.DefaultTransPipeline;
import com.nanyang.common.core.flow.invoker.TransPipeline;
import com.nanyang.tms.api.dto.request.bill.BillSolveRequest;
import com.nanyang.tms.domain.bill.context.BillSolveContext;
import com.nanyang.tms.manager.bill.handler.BillBuildHandler;
import com.nanyang.tms.manager.bill.handler.BillFeeHandler;
import com.nanyang.tms.manager.bill.handler.BillSolveWaybillHandler;
import com.nanyang.tms.manager.bill.handler.BillTransactionRecordHandler;
import com.nanyang.tms.manager.cost.handler.CalculateCostFreightHandler;
import com.nanyang.tms.manager.forecast.handler.CalculateFreightHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BillProcessPipelineFactory extends AbstranctTransPipelineFactory<BillSolveRequest>{
    @Autowired
	private BillBuildHandler billBuildHandler;
    @Autowired
    private CalculateFreightHandler calculateFreightHandler;
	@Autowired
	private CalculateCostFreightHandler calculateCostFreightHandler;
    @Autowired
    private BillTransactionRecordHandler billTransactionRecordHandler;
    @Autowired
    private BillFeeHandler billFeeHandler;
	@Autowired
	private BillSolveWaybillHandler billSolveWaybillHandler;
	@Override
	public TransHandlerContext createContext() {
		return new BillSolveContext();
	}

	@Override
	public void doBuild(TransPipeline pipeline) {
		pipeline.addLast(billBuildHandler); //组织数据
		pipeline.addLast(calculateFreightHandler);//计算销售运费
		pipeline.addLast(calculateCostFreightHandler);//计算成本运费
		pipeline.addLast(false,billFeeHandler);//收入支出记录
		pipeline.addLast(false,billSolveWaybillHandler);//更新运单信息
		pipeline.addLast(true,billTransactionRecordHandler);//动账记录
	}

	@Override
	public TransPipeline createPipeline(TransHandlerContext context) {
		 //所有handler 都没有事务 用这个构造函数
		//return new DefaultTransPipeline(context);
         //所有handler中有一个有事务就需要用 这个构造函数
		return new DefaultTransPipeline(context,super.getDataSourceTransactionManager(),super.getTransactionDefinition()); 
	}

	@Override
	public TransConvert createConvert() {
		return new BillConvert();
	}

}
